<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺编号" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入商铺编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:shop_keyword:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:shop_keyword:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:shop_keyword:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:shop_keyword:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shop_keywordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商铺编号" align="center" prop="shopId" />
      <el-table-column label="环境标题" align="center" prop="wordTitle" />
      <el-table-column label="环境图片" align="center" prop="wordShow" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.wordShow"
            :preview-src-list="[scope.row.wordShow]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:shop_keyword:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:shop_keyword:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商铺环境展示对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="环境标题" prop="wordTitle">
          <el-input v-model="form.wordTitle" placeholder="请输入环境标题" />
        </el-form-item>
        <el-form-item label="环境封面" prop="wordShow">
          <el-upload
            class="avatar-uploader"
            :action="fileUrl"
            :show-file-list="false"
            :on-success="successFile"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.wordShow" :src="form.wordShow" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>        </el-form-item>
        <el-form-item label="文案内容" prop="wordText">
          <editor v-model="form.wordText" :min-height="99" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShop_keyword, getShop_keyword, delShop_keyword, addShop_keyword, updateShop_keyword, exportShop_keyword } from "@/api/admin/shop_keyword";
import { fileUrl } from "@/constant/constant";
import Editor from "@/components/Editor";

export default {
  name: "Shop_keyword",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      fileUrl : '',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺环境展示表格数据
      shop_keywordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: null,

        wordTitle: null,

        wordShow: null,

        wordText: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wordTitle: [
          { required: true, message: "环境标题不能为空", trigger: "blur" }
        ],

        wordShow: [
          { required: true, message: "封面不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
    this.fileUrl = fileUrl;
  },
  methods: {
    /** 查询商铺环境展示列表 */
    getList() {
      this.loading = true;
      listShop_keyword(this.queryParams).then(response => {
        this.shop_keywordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        shopId: null,

        wordTitle: null,

        wordShow: null,

        wordText: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    successFile(res, file) {
      this.form.wordShow = URL.createObjectURL(file.raw);
      this.form.wordShow = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商铺环境展示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShop_keyword(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商铺环境展示";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShop_keyword(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop_keyword(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商铺环境展示编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delShop_keyword(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商铺环境展示数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportShop_keyword(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.topStyle {
  padding-left: 30px;
}
</style>

<style lang="scss" scoped>
.shopPoi {
  height: 700px;
  width: 1000px;
}
.el-vue-search-box-container {
  width: 100% !important;
  margin-bottom: 10px;
}
</style>
